<?php
/**
 *
 * @author ninteySeven.
 * @date 2022-12-12 0:32
 */

use Illuminate\Support\Facades\Log;

if (!function_exists('success')) {
    function success($data = [],$source=null)
    {
        $headers = array(
            'Content-Type' => 'application/json; charset=utf-8',
            'Inner-machine' => gethostname()
        );
        if(!is_null($source))
            $headers['Data-source'] = $source;

        $result = [
            'code' => 200,
            'message' => 'success',
            'data' => !empty($data) ? $data : [],
        ];

        return response()->json($result,200,$headers,JSON_UNESCAPED_UNICODE);
    }
}

if (!function_exists('fail')) {
    function fail($code, $message, $data = [])
    {
        $headers = array(
            'Content-Type' => 'application/json; charset=utf-8',
            'Inner-machine' => gethostname()
        );
        $result = [
            'code' => $code,
            'message' => $message,
            'data' => !empty($data) ? $data : [],
        ];
        Log::error(sprintf("统一失败log：%s, url=%s",$message, var_export($data,true)));

        return response()->json($result,200,$headers,JSON_UNESCAPED_UNICODE);
    }
}

if (!function_exists('randSmsCode')) {
    function randSmsCode()
    {
        $chars = '0123456789';
        $code = '';
        while (strlen($code) < 4) {
            $code .= substr($chars, (mt_rand() % strlen($chars)), 1);
        }
        return $code;
    }
}

if (!function_exists('timeDiff')) {
    function timeDiff($begin_time, $end_time)
    {
        if ($begin_time < $end_time) {
            $starttime = $begin_time;
            $endtime = $end_time;
        } else {
            $starttime = $end_time;
            $endtime = $begin_time;
        }

        //计算天数
        $timediff = $endtime - $starttime;
        $days = intval($timediff / 86400);
        //计算小时数
        $remain = $timediff % 86400;
        $hours = intval($remain / 3600);
        //计算分钟数
        $remain = $remain % 3600;
        $mins = intval($remain / 60);
        //计算秒数
        $secs = $remain % 60;
        $res = array("day" => $days, "hour" => $hours, "min" => $mins, "sec" => $secs);
        return $res;
    }
}

if (!function_exists('md5Password')) {
    function md5Password($password): string
    {
        $halt = 'sxcx2023';
        return md5($halt . sha1($password));
    }
}

if (!function_exists('curlGet')) {
    /**
     *模拟GET请求
     **/
    function curlGet($url,$headers=[])
    {
        $headerArray = array("Content-type:application/json;", "Accept:application/json");
        if( empty($headerArray) ) $headers = $headerArray;
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        $output = curl_exec($ch);
        curl_close($ch);
        return json_decode($output, true);
    }
}

if (!function_exists('curlPost')) {
    /**
     * CURL的Post请求方法
     * @param string $url
     * @param string $param
     * @return bool|string
     */
    function curlPost(string $url, string $params,$headers=[],$method='POST')
    {
        // 初始化curl
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        // 要求结果为字符串且输出到屏幕上
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        // post提交方式
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
        if( !empty($params))     curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
        if(!empty($headers))     curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);

        // 运行curl
        $data = curl_exec($curl);
        curl_close($curl);

        return json_decode($data, true);
    }
}
if (!function_exists('del_redis_keys')) {
    /**
     * CURL的Post请求方法
     * @param string $url
     * @param string $param
     * @return bool|string
     */
    function del_redis_keys(string $redisOrderKeys = '')
    {
        // 取redis
        $redis = app('redis.connection');
        $redisData = $redis->get($redisOrderKeys);
        if( !empty($redisData) ){
            $keys = json_decode($redisData,true);
            $keys[] = $redisOrderKeys;
            foreach( $keys as $key )
                $redis->del($key);
        }
    }
}
